/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package chart;

import javax.swing.JFrame;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.LegendItem;
import org.jfree.chart.LegendItemCollection;
import org.jfree.chart.axis.AxisLocation;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.axis.NumberTickUnit;
import org.jfree.chart.labels.StandardCategoryToolTipGenerator;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.renderer.category.BarRenderer;
import org.jfree.chart.renderer.category.CategoryItemRenderer;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;

import data_access.ResultObj;

/**
 * 
 * @author Anh
 */
public class BarChart extends JFrame {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public BarChart() {

	}

	public BarChart(ResultObj rsObj) {
		final CategoryDataset dataset1 = createDataset1(rsObj);
		final CategoryDataset dataset2 = createDataset2(rsObj);
		final JFreeChart chart = createChart(dataset1, dataset2, rsObj);
		final ChartPanel chartPanel = new ChartPanel(chart);
		chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));

		setContentPane(chartPanel);
	}

	/**
	 * Creates a sample dataset.
	 * 
	 * @return The dataset.
	 */
	private CategoryDataset createDataset1(ResultObj rsObj) {

		// row keys...
		final String series1 = "Accuracy";
		final String series2 = "Dummy 1";

		// column keys...
		final String category1 = "SVM_OAO";
		final String category2 = "SVM_DDAG";
		final String category3 = "SVM_ADAG";

		// create the dataset...
		final DefaultCategoryDataset dataset = new DefaultCategoryDataset();

		dataset.addValue(rsObj.getAccOAO(), series1, category1);
		dataset.addValue(rsObj.getAccDDAG(), series1, category2);
		dataset.addValue(rsObj.getAccADAG(), series1, category3);

		dataset.addValue(null, series2, category1);
		dataset.addValue(null, series2, category2);
		dataset.addValue(null, series2, category3);

		return dataset;

	}

	/**
	 * Creates a sample dataset.
	 * 
	 * @return The dataset.
	 */
	private CategoryDataset createDataset2(ResultObj rsObj) {

		// row keys...
		final String series1 = "Dummy 2";
		final String series2 = "Time";

		// column keys...
		final String category1 = "SVM_OAO";
		final String category2 = "SVM_DDAG";
		final String category3 = "SVM_ADAG";

		// create the dataset...
		final DefaultCategoryDataset dataset = new DefaultCategoryDataset();

		dataset.addValue(null, series1, category1);
		dataset.addValue(null, series1, category2);
		dataset.addValue(null, series1, category3);

		dataset.addValue(rsObj.getTimeOAO(), series2, category1);
		dataset.addValue(rsObj.getTimeDDAG(), series2, category2);
		dataset.addValue(rsObj.getTimeADAG(), series2, category3);

		return dataset;

	}

	/**
	 * Creates a chart.
	 * 
	 * @param dataset1
	 *            the first dataset.
	 * @param dataset2
	 *            the second dataset.
	 * 
	 * @return A chart.
	 */
	private JFreeChart createChart(final CategoryDataset dataset1, final CategoryDataset dataset2, ResultObj rsObj) {

		final CategoryAxis domainAxis = new CategoryAxis("Method");
		domainAxis.setCategoryMargin(.4);
		final NumberAxis rangeAxis = new NumberAxis("Accuracy(%)");
		rangeAxis.setRange(0, 100);
		rangeAxis.setTickUnit(new NumberTickUnit(10));
		final BarRenderer renderer1 = new BarRenderer();
		renderer1.setBaseToolTipGenerator(new StandardCategoryToolTipGenerator());
		final CategoryPlot plot = new CategoryPlot(dataset1, domainAxis, rangeAxis, renderer1) {

			/**
			 * 
			 */
			private static final long serialVersionUID = 1L;

			/**
			 * Override the getLegendItems() method to handle special case.
			 * 
			 * @return the legend items.
			 */
			public LegendItemCollection getLegendItems() {

				final LegendItemCollection result = new LegendItemCollection();

				final CategoryDataset data = getDataset();
				if (data != null) {
					final CategoryItemRenderer r = getRenderer();
					if (r != null) {
						final LegendItem item = r.getLegendItem(0, 0);
						result.add(item);
					}
				}

				// the JDK 1.2.2 compiler complained about the name of this
				// variable
				final CategoryDataset dset2 = getDataset(1);
				if (dset2 != null) {
					final CategoryItemRenderer renderer2 = getRenderer(1);
					if (renderer2 != null) {
						final LegendItem item = renderer2.getLegendItem(1, 1);
						result.add(item);
					}
				}

				return result;

			}

		};

		final JFreeChart chart = new JFreeChart("Bar Chart", plot);
		// chart.setBackgroundPaint(Color.white);
		// chart.getLegend().setAnchor(Legend.SOUTH);
		// plot.setBackgroundPaint(new Color(0xEE, 0xEE, 0xFF));
		plot.setDomainAxisLocation(AxisLocation.BOTTOM_OR_RIGHT);
		plot.setDataset(1, dataset2);
		plot.mapDatasetToRangeAxis(1, 1);

		final NumberAxis axis2 = new NumberAxis("Time(ms)");
		int timer = rsObj.getTimeOAO() > rsObj.getTimeDDAG() ? (rsObj.getTimeOAO() > rsObj.getTimeADAG() ? rsObj
				.getTimeOAO() : rsObj.getTimeADAG()) : (rsObj.getTimeDDAG() > rsObj.getTimeADAG() ? rsObj.getTimeDDAG()
				: rsObj.getTimeADAG());

		int height = 1;
		int temp = timer;
		while (timer > 10) {
			timer = timer / 10;
			height *= 10;
		}
		temp = (height + temp) / height * height;
		axis2.setRange(0, temp);
		axis2.setTickUnit(new NumberTickUnit(temp / 10));
		plot.setRangeAxis(1, axis2);

		plot.setRangeAxisLocation(1, AxisLocation.BOTTOM_OR_RIGHT);
		final BarRenderer renderer2 = new BarRenderer();
		renderer2.setBaseToolTipGenerator(new StandardCategoryToolTipGenerator());
		plot.setRenderer(1, renderer2);

		return chart;
	}
}
